<?php
/**
 * 系统文章
 *
 *
 *
 ***/


defined('IN_NWIDC') or exit('Access Invalid!');

class documentControl extends BaseHomeControl {
	public function indexOp(){
		$lang	= Language::getLangContent();
		if($_GET['code'] == ''){
			showMessage($lang['para_error'],'','html','error');//'缺少参数:文章标识'
		}
		$model_doc	= Model('document');
		$doc	= $model_doc->getOneByCode($_GET['code']);
		Tpl::output('doc',$doc);
		/**
		 * 分类导航
		 */
		$nav_link = array(
			array(
				'title'=>$lang['homepage'],
				'link'=>SHOP_SITE_URL
			),
			array(
				'title'=>$doc['doc_title']
			)
		);
		Tpl::output('nav_link_list',$nav_link);
		Tpl::showpage('document.index');
	}


	function notifyOp(){
        
        require_once dirname(BASE_PATH) . '/wxpay/lib/WxPay.Api.php';
        file_put_contents('paysuccess.txt',print_r($GLOBALS,true));

        WxPayApi::notify(function($returnMsg){

            if($returnMsg['return_code'] != 'SUCCESS'){
                exit;
            }

            //检查微信参数
            $wxinfo = Model('payment')->where(array('payment_code' => 'wxpay'))->find();
            $wxinfo = unserialize($wxinfo);
            if($returnMsg['appid'] != $wxinfo['appid'] || $returnMsg['mch_id'] != $wxinfo['mch_id']){
                exit;
            }
            
            $order_sn = substr($returnMsg['out_trade_no'],0,-10);
            $order_info = Model()->table('order')->where(array('order_sn' => $order_sn))->find();
            //检查订单金额
            if($order_info['order_amount'] != $returnMsg['total_fee']){
                exit;
            }
            //检查订单状态
            if($order_info['order_state'] != 10){
                exit;
            }

            $pay_sn = $order_info['pay_sn'];
            $logic_payment = Logic('payment');
            $result = $logic_payment->getRealOrderInfo($pay_sn);
            $order_list = $result['data']['order_list'];
            $result = $logic_payment->updateRealOrder($order_sn, '微信支付', $order_list, $returnMsg['transaction_id']);
            if($result['state']){
                // Model()->table('order')->where(array('order_sn' => $order_sn))->update(array('order_state' => 20,'payment_time' => time(),'payment_code' => 'wxpay'));
                // Model()->table('order_pay')->where(array('pay_sn' => $order_info['pay_sn']))->update(array('api_pay_state' => 1));
                echo 'SUCCESS';
            }
        });

        exit;
    }
}
